package com.societyGames.flashForms
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class TextButton extends ThreeStateButton
	{
		private var _desiredWidth:Number;
		private var _desiredHeight:Number;
		private var _textField:TextField;
		
		public function TextButton(up:Sprite, over:Sprite = null, down:Sprite = null, textField:TextField = null)
		{
			super(up, over, down);
			this._textField = textField;
			//If we don't have a textfield, create a new default one.
			if (this._textField == null)
			{
				this._textField = new TextField();
				var textFormat:TextFormat = new TextFormat();
				textFormat.align = TextFormatAlign.CENTER;
				this._textField.defaultTextFormat = textFormat;
			}
			this._textField.width = this.width;
			this._textField.height = this.height;
			this.addChild(this._textField);
		}
		
		public override function set height(value:Number):void
		{
			super.height = value;
			this._desiredHeight = value;
			recalculateHeight();
		}
		
		public override function set width(value:Number):void
		{
			super.width = value;
			this._desiredWidth = value;
			this._textField.width = value;
			recalculateHeight(); //because the text may have wrapped.
		}
		
		public function set text(value:String):void
		{
			this._textField.text = value;
			recalculateHeight();
		}
		
		public function get text():String
		{
			return this._textField.text;
		}
		
		private function recalculateHeight():void
		{
			this._textField.y = (this._desiredHeight - this._textField.textHeight) / 2; //Center it.
		}
	}
}